package com.Guo.aop;

import org.aopalliance.intercept.Joinpoint;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

/**
 * @author Admin
 * @project UserHandler
 * @description
 * @date 2020-10-27-10:11
 */
@Aspect
public class UserHandler {
    private static Logger logger = Logger.getLogger(UserHandler.class);

    @Pointcut("execution(* com.Guo.Service..*.*(..))")
    public void pointcut() {
    }

    @Before("pointcut()")
    public void before(JoinPoint joinPoint) {
        System.out.println("方法参数是:" + joinPoint.getArgs() + "方法名称为:" + joinPoint.getSignature().getName());
    }

    @AfterReturning(pointcut = "pointcut()", returning = "result")
    public void after(JoinPoint joinPoint, Object result) {
        System.out.println("后置处理" + "，返回值为:" + result);
    }

    @After("pointcut()")
    public void finallyAfter() {
        System.out.println("最终增强");
    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) {
        System.out.println("方法参数是:" + joinPoint.getArgs() + "方法名称为:" + joinPoint.getSignature().getName());
        try {
            //后置增强
            //proceed()方法可以调用真正的目标方法，从而实现对连接点的完全控制
            Object proceed = joinPoint.proceed();
            System.out.println("调用了" + joinPoint.getTarget() + "的" + joinPoint.getSignature().getName() + "返回值是:" + proceed);
            return proceed;
        } catch (Throwable throwable) {
            System.out.println("出现了异常");
            throwable.printStackTrace();
        } finally {
            System.out.println("方法执行完毕");
        }
        return null;
    }
}
